package com.vitabuybuy.tv.ui.shopcenter.store

import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import com.library.common.base.BaseActivity
import com.library.common.dto.MessageEvent
import com.library.common.ext.mmkv
import com.library.common.util.FragmentUtils
import com.library.common.util.MMKVContancts
import com.tencent.imsdk.TIMConversationType
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityOrderDetailBinding
import com.vitabuybuy.tv.ui.auth.LoginActivity
import com.vitabuybuy.tv.ui.shopcenter.store.viewmodel.StoreHomeViewModel
import com.vitabuybuy.tv.ui.tencent.chat.ChatActivity
import com.vitabuybuy.tv.utils.EventCode
import kotlinx.android.synthetic.main.activity_store_home.*

class StoreHomeActivity : BaseActivity<StoreHomeViewModel, ActivityOrderDetailBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_store_home

    private var storeId: Int? = 0
    private var storeName: String? = ""
    private var type: Int? = -1
    override fun onGetBundle(bundle: Bundle?) {
        storeId = bundle?.getInt("storeId")
        storeName = bundle?.getString("storeName")
        type = bundle?.getInt("type")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(storeName ?: "")
        mIvRight.apply {
            setImageResource(R.drawable.icon_navbar_share)
            visibility = View.VISIBLE
            setOnClickListener { }
        }
        if (type == JUMP_CATE_PAGE) {
            navView.selectedItemId = R.id.navigationCommodity
            showCurrentFragment(1)
        } else {
            showCurrentFragment(0)
        }
    }

    fun getStoreId(): Int? {
        return storeId
    }

    fun getJumpType(): Int? {
        return type
    }

    override fun eventHandling() {
        navView.setOnNavigationItemSelectedListener { menuItem ->
            when (menuItem.itemId) {
                R.id.navigationShopHome -> {
                    showCurrentFragment(0)
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.navigationCommodity -> {
                    showCurrentFragment(1)
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.navigationClassification -> {
                    showCurrentFragment(2)
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.navigationCustomerService -> {
                    if (!mmkv.decodeBool(MMKVContancts.IS_LOGIN_IN, false)) {
                        LoginActivity.openActivity(mContext)
                        return@setOnNavigationItemSelectedListener false
                    }
                    val chatInfo = ChatInfo()
                    chatInfo.type = TIMConversationType.C2C
                    chatInfo.id = storeId.toString()
                    chatInfo.chatName = storeName
                    ChatActivity.openActivity(mContext, chatInfo)
                }
            }
            false
        }
    }

    private fun showCurrentFragment(index: Int) {
        when (index) {
            0 -> FragmentUtils.replaceFragment(
                supportFragmentManager,
                R.id.containerFragment,
                StoreHomeFragment::class.java.name
            )
            1 -> FragmentUtils.replaceFragment(
                supportFragmentManager,
                R.id.containerFragment,
                StoreCommodityFragment::class.java.name
            )
            2 -> FragmentUtils.replaceFragment(
                supportFragmentManager,
                R.id.containerFragment,
                StoreClassificationFragment::class.java.name
            )
        }
    }

    override fun onEvent(event: MessageEvent) {
        when (event.code) {
            EventCode.jump_store_cate -> {
                navView.selectedItemId = R.id.navigationClassification
            }
        }
    }


    companion object {
        const val JUMP_CATE_PAGE = 0X11
        const val JUMP_BIDING_PAGE = 0X12
        const val JUMP_GROUP_PAGE = 0X13

        fun openActivity(mContext: BaseActivity<*, *>, storeId: Int?, storeName: String?) {
            mContext.startActivity(
                bundleOf(
                    Pair("storeId", storeId),
                    Pair("storeName", storeName)
                ), StoreHomeActivity::class.java
            )
        }

        fun openActivity(
            mContext: BaseActivity<*, *>,
            storeId: Int?,
            storeName: String?,
            type: Int
        ) {
            mContext.startActivity(
                bundleOf(
                    Pair("storeId", storeId),
                    Pair("storeName", storeName),
                    Pair("type", type)
                ), StoreHomeActivity::class.java
            )
        }
    }
}
